﻿using System;
using System.Collections.Generic;

// 学生类
class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string StudentId { get; set; }

    public Student(string name, int age, string studentId)
    {
        Name = name;
        Age = age;
        StudentId = studentId;
    }

    public override string ToString()
    {
        return $"学号: {StudentId}, 姓名: {Name}, 年龄: {Age}";
    }
}

// 班级类，实现索引器
class Class
{
    private List<Student> students;

    public string ClassName { get; set; }
    public int Capacity { get; private set; }

    public Class(string className, int capacity)
    {
        ClassName = className;
        Capacity = capacity;
        students = new List<Student>(capacity);
    }

    // 初始化班级
    public void InitClass()
    {
        students = new List<Student>(Capacity);
    }

    // 清空班级
    public void ClearClass()
    {
        students.Clear();
    }

    // 添加学生
    public bool AddStudent(Student student)
    {
        if (students.Count >= Capacity)
        {
            Console.WriteLine("班级已满，无法添加学生");
            return false;
        }

        students.Add(student);
        return true;
    }

    // 移除学生
    public Student RemoveStudent(int index)
    {
        if (index < 0 || index >= students.Count)
        {
            throw new IndexOutOfRangeException("索引超出范围");
        }

        Student student = students[index];
        students.RemoveAt(index);
        return student;
    }

    // 索引器 - 实现按索引访问学生
    public Student this[int index]
    {
        get
        {
            if (index < 0 || index >= students.Count)
            {
                throw new IndexOutOfRangeException("索引超出范围");
            }
            return students[index];
        }
        set
        {
            if (index < 0 || index >= students.Count)
            {
                throw new IndexOutOfRangeException("索引超出范围");
            }
            students[index] = value;
        }
    }

    // 通过学号索引器
    public Student this[string studentId]
    {
        get
        {
            foreach (Student student in students)
            {
                if (student.StudentId == studentId)
                {
                    return student;
                }
            }
            throw new KeyNotFoundException($"未找到学号为 {studentId} 的学生");
        }
    }

    // 学生数量
    public int Count
    {
        get { return students.Count; }
    }

    // 显示班级所有学生
    public void DisplayAllStudents()
    {
        Console.WriteLine($"--- {ClassName} 班级学生列表 ---");
        for (int i = 0; i < students.Count; i++)
        {
            Console.WriteLine($"{i}: {students[i]}");
        }
        Console.WriteLine($"共 {students.Count} 名学生");
    }
}

class Program
{
    static void Main()
    {
        // 创建班级
        Class myClass = new Class("计算机科学与技术1班", 5);

        // 初始化班级
        myClass.InitClass();

        // 添加学生
        myClass.AddStudent(new Student("张三", 20, "2023001"));
        myClass.AddStudent(new Student("李四", 21, "2023002"));
        myClass.AddStudent(new Student("王五", 19, "2023003"));

        Console.WriteLine($"班级中学生数量: {myClass.Count}");

        // 使用索引器访问学生
        Console.WriteLine("\n通过索引访问学生:");
        Console.WriteLine($"第一个学生: {myClass[0]}");
        Console.WriteLine($"第二个学生: {myClass[1]}");

        // 通过学号访问学生
        Console.WriteLine("\n通过学号访问学生:");
        Console.WriteLine($"学号2023002的学生: {myClass["2023002"]}");

        // 使用索引器修改学生信息
        Console.WriteLine("\n修改第二个学生信息:");
        myClass[1] = new Student("赵六", 22, "2023004");
        Console.WriteLine($"修改后的第二个学生: {myClass[1]}");

        // 显示所有学生
        myClass.DisplayAllStudents();

        // 移除学生
        Console.WriteLine("\n移除第一个学生:");
        Student removedStudent = myClass.RemoveStudent(0);
        Console.WriteLine($"移除的学生: {removedStudent}");

        // 再次显示所有学生
        myClass.DisplayAllStudents();

        // 清空班级
        myClass.ClearClass();
        Console.WriteLine($"\n清空后学生数量: {myClass.Count}");
    }
}